home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / wildmat.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  81 lines

  1. /*
  2.  * adapted from FORMS library routines which were in turn
  3.  * adapted from Rich Salz
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #ifndef FALSE
  11. #define FALSE 0
  12. #define TRUE  1
  13. #endif
  14.  
  15. #include "ulib.h"
  16.  
  17. static int match_star(const char *s, const char *p);
  18.  
  19. static int
  20. do_matching(register const char *s, register const char *p)
  21. /* match string "s" to pattern "p" */
  22. {
  23.     register int last, matched, reverse;
  24.  
  25.     for (; *p; s++, p++)
  26.       {
  27.       if (*s == '\0')
  28.           return (*p == '*' && *++p == '\0' ? TRUE : -1);
  29.       switch (*p)
  30.         {            /* parse pattern */
  31.         case '\\':        /* Literal match with following character. */
  32.         if (*s != *++p)
  33.             return FALSE;
  34.         continue;
  35.         default:        /* literal match */
  36.         if (*s != *p)
  37.             return FALSE;
  38.         continue;
  39.         case '?':        /* Match anything. */
  40.         continue;
  41.         case '*':        /* Trailing star matches everything. */
  42.         return (*++p ? match_star(s, p) : TRUE);
  43.         case '[':        /* [!....] means inverse character class. */
  44.         if ((reverse = p[1] == '!'))
  45.             p++;
  46.         for (last = 0400, matched = FALSE; *++p && *p != ']'; last = *p)
  47.             /* This next line requires a good C compiler. */
  48.             /* range?        (in bounds) (equal) */
  49.             if ((*p == '-') ? (*s <= *++p && *s >= last) : (*s == *p))
  50.             matched = TRUE;
  51.         if (matched == reverse)
  52.             return FALSE;
  53.         continue;
  54.         }
  55.       }
  56.     return *s == '\0';
  57. }
  58. static int
  59. match_star(const char *s, const char *p)
  60. {
  61.     int result;
  62.  
  63.     while ((result = do_matching(s, p)) == FALSE)    /* gobble up * match */
  64.     if (*++s == '\0')
  65.         return -1;
  66.     return result;
  67. }
  68.  
  69. int
  70. wildmat(const char *s, const char *p)
  71. {
  72.     if (*p == '\0' && *s != '.')
  73.     return TRUE;
  74.     else if (*p == '\0')
  75.     return FALSE;
  76.     else if ((*p == '?' || *p == '*') && *s == '.')
  77.     return FALSE;
  78.     else
  79.     return do_matching(s, p) == TRUE;
  80. }
  81.